// components/UploadImages/UploadImages.js
import { uploadFile } from '../../service/common-service.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: Array,
      observer: function (newData, oldData) {
        const images = newData.map(item=>{
          return item.fileUrl
        });

        this.setData({ images })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    images:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    upload: function () {
      const _ = this;
      wx.chooseImage({
        count: 5,
        success: function (res) {
          if (res.errMsg === "chooseImage:ok") {
            for (let i = 0; i < res.tempFilePaths.length; i++){
              uploadFile(res.tempFilePaths[i]).then(function (result) {
                const { status, message, data } = result;
                if (status === 0) {
                  const { images } = _.data;
                  images.push(data.fileUrl)
                  _.setData({ images: images });
                  _.triggerEvent('change', {
                    images: images
                  })
                }
              });
            }
          }
        }
      })
    }
  }
})
